﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BesAsm.Framework.Reporting
{
  public static class MessageQueue
  {
    private static Queue<ReportableMessage> _messages = new Queue<ReportableMessage>();
    internal static event MessageReportedEventHandler MessageQueued;

    public static void AddMessenger(IMessenger messenger)
    {
      if (messenger != null)
        messenger.MessageReported += new MessageReportedEventHandler(messenger_MessageReported);
    }

    public static void RemoveMessenger(IMessenger messenger)
    {
      if (messenger != null)
        messenger.MessageReported -= messenger_MessageReported;
    }

    private static void messenger_MessageReported(object sender, MessageReportedArgs e)
    {
      _messages.Enqueue(e.Message);
      OnMessageQueued(e);
    }

    internal static void OnMessageQueued(MessageReportedArgs e)
    {
      if (MessageQueued != null)
        MessageQueued(null, e);
    }

    internal static ReportableMessage GetMessage()
    {
      if (_messages.Count > 0)
        return _messages.Dequeue();
      else
        return null;
    }

    internal static int MessageCount { get { return _messages.Count; } }
  }
}
